#if 0
#include<sys/socket.h>
#include<ctype.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<thread>
#include<string.h>
#include <sys/epoll.h>
 #include <unistd.h>
#define SERVICEPORT 8080
#define OPEN_SIZE 1024
#define ThreadNum 5
#include<iostream>
using namespace std;
struct epoll_event tempevent_1,ArrEvents_1[OPEN_SIZE];
int epfd_1;
void fun(){
   int i;
   while (1)
   {
      int nums1=epoll_wait(epfd_1,ArrEvents_1,OPEN_SIZE,-1);
      for(i=0;i<nums1;i++){
          int events=ArrEvents_1[i].events;
          if(events & EPOLLIN){
              char buf[1024]={0};
              int n=read(ArrEvents_1[i].data.fd,buf,sizeof(buf));
              cout<<"当前epoll树节点:"<<epfd_1<<endl;
              cout<<"当前文件描述符:"<<ArrEvents_1[i].data.fd<<endl;
              cout<<buf<<endl;
              ArrEvents_1->events=EPOLLOUT;
              epoll_ctl(epfd_1,EPOLL_CTL_MOD,ArrEvents_1[i].data.fd,ArrEvents_1);
          }
          else if(events & EPOLLOUT){
              cout<<"写事件触发"<<endl;
              char buf[]="草泥马";
              write(ArrEvents_1[i].data.fd,buf,sizeof(buf));
              cout<<"关闭文件描述符："<<ArrEvents_1[i].data.fd<<endl;
              close(ArrEvents_1[i].data.fd);
          }
          else if(events &EPOLLRDHUP){
             //return ;
          }
      }
   }     
}

int main(){
   int listenfd;
   listenfd=socket(AF_INET,SOCK_STREAM,0);//创建套接字
   if(listenfd==-1){
      //sys_error("socket is error");
   }
 
   //绑定地址结构
   struct sockaddr_in saddr,caddr;
   saddr.sin_port=htons(SERVICEPORT);
   saddr.sin_addr.s_addr=htonl(INADDR_ANY);
   saddr.sin_family=AF_INET;
   bind(listenfd,(struct sockaddr*)&saddr,sizeof(saddr));
   //监听
   listen(listenfd,128);
   //epoll实现IO多路复用
   int epfd,nums,i,connfd,sockfd,n,j;
   socklen_t caddr_len;
   char buf[1024];//创建缓冲区
   struct epoll_event tempevent,ArrEvents[OPEN_SIZE];
#if 1
   ArrEvents->events=EPOLLIN;
   ArrEvents->data.fd=listenfd;
   epfd= epoll_create(OPEN_SIZE);//创建红黑树
   epfd_1= epoll_create(OPEN_SIZE);//创建红黑树
   if(epfd==-1){
      //sys_error("epoll_create is error");
   }
   int ret=epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,ArrEvents);
   if(ret==-1){
      //sys_error("epoll_ctl is error");
   }
#else
   tempevent.events=EPOLLIN;
   tempevent.data.fd=listenfd;
   epfd= epoll_create(OPEN_SIZE);//创建红黑树
   epfd_1= epoll_create(OPEN_SIZE);//创建红黑树
   if(epfd==-1){
      //sys_error("epoll_create is error");
   }
   //将节点添加到红黑树上
   int ret=epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&tempevent);
   if(ret==-1){
      //sys_error("epoll_ctl is error");
   }
#endif
   thread pa(fun);
   while(1){
      //阻塞监听
      nums=epoll_wait(epfd,ArrEvents,OPEN_SIZE,-1);
     
      if(nums<0){
         //sys_error("epoll_wait is error");
      }
      for(i=0;i<nums;i++){
         if(!(ArrEvents[i].events & EPOLLIN)){//如果不是读事件继续循环
           continue;
	     }
      if(ArrEvents[i].data.fd==listenfd){//又客户端链接
            caddr_len=sizeof(caddr);
            connfd=accept(listenfd,(struct sockaddr*)&caddr,&caddr_len);//有连接	   
            cout<<"当前epoll树节点:"<<epfd<<endl;
            cout<<"新用户连接"<<connfd<<endl;
            tempevent_1.data.fd=connfd;
            tempevent_1.events=EPOLLIN;
            epoll_ctl(epfd_1,EPOLL_CTL_ADD,connfd,&tempevent_1);
            cout<<"主线程id:"<<this_thread::get_id()<<endl;
	 }else{            
	    sockfd=ArrEvents[i].data.fd;
        n=read(sockfd,buf,sizeof(buf));
	    if(n==-1){
               //sys_error("read is error");
	    }
	    if(n==0){
	       epoll_ctl(epfd,EPOLL_CTL_DEL,sockfd,NULL);    

           close(sockfd);
        }
        cout<<buf;
        
        memset(buf,0,1024);
     }
     }
   }
   close(listenfd);
   return 0;
}

// int main111(){

//   int n=1;
//   int n1=2;
//   int ret=n&n1;  //与 有零为零 
//   int ret1=n|n1;  //或 有1为1 
//   cout<<ret1<<endl;
//   return 0;
// }
// EpollReactor *task_queue=new EpollReactor[5];
// static int num=0;
// void task_thread(EpollReactor *task_queue){   //任务线程
//       task_queue->run();
      
// }
// struct arg{
//    int fd;
//    EpollReactor *arg1;
//    FdInfo f;
// };
// int fun3(void*ar){
//    arg*temp=(arg *)ar;
//    cout<<"写数据"<<temp->fd<<endl;
//    return 0;
// }
// int fun2(void*ar){
//    arg*temp=(arg *)ar;
//    cout<<"当前epoll树节点"<<temp->arg1->getfd()<<endl;
//    char buf[1024]="0";
//    int n=read(temp->fd,buf,sizeof(buf));
//    if(n==0){
//       return 0;
//    }
//    temp->f.changeEvent(Event::WRITEEVENT);
//    temp->arg1->setfdinfo(temp->f);
//    temp->arg1->modify();
//    cout<<buf;
//    return 1;
// }
// int fun1(void*ar){
//    arg*temp=(arg *)ar;
//    int connfd=accept(temp->fd,nullptr,nullptr);//有连接
//    cout<<"有连接"<<connfd<<endl;
//    arg *a=new arg;
//    a->fd=connfd;
//    a->arg1=&task_queue[num%5];
//    FdInfo f(connfd,1,fun2,fun3,nullptr,(void*)a);
//    a->f=f;
//    task_queue[num%5].setfdinfo(f);
//    //task_queue[num%5].addFdinfo(f);
//    task_queue[num%5].add();
//    num++;
//    return 1;
// }

// int main(){

//    int listenfd=socket(AF_INET,SOCK_STREAM,0);//创建套接字
//    if(listenfd==-1){
//       //sys_error("socket is error");
//    }

//    //设置端口复用
//    int opt=1;
//    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

//    //绑定地址结构
//    struct sockaddr_in saddr,caddr;
//    saddr.sin_port=htons(SERVICEPORT);
//    saddr.sin_addr.s_addr=htonl(INADDR_ANY);
//    saddr.sin_family=AF_INET;
//    bind(listenfd,(struct sockaddr*)&saddr,sizeof(saddr));
//    //监听
//    listen(listenfd,128);
  
//    EpollReactor *rea=new EpollReactor();
//    for(int i=0;i<5;i++){
//       thread*pa=new thread(task_thread,&task_queue[i]);
//    }
//    arg *a=new arg;
//    a->fd=listenfd;
//    a->arg1=rea;

//    int event=static_cast<int>(Event::READEVENT);
//    FdInfo info(listenfd,event,fun1,nullptr,nullptr,(void*)a);


//    rea->setfdinfo(info); 
//    rea->add();

//    rea->run();
//    return 0;
// }

#else
#include"MusicServer.h"
#include<string.h>
#define SERVICEPORT 8080
#define ThreadNum 5
int main(){
   MusicServer*server=new MusicServer(SERVICEPORT,ThreadNum);
   server->Run();
   return 0;
}
#endif